package island.gotenyama.rabbit.controller;

import island.gotenyama.wand.bean.DataRow;
import island.gotenyama.wand.bean.QueryParams;
import island.gotenyama.wand.field.PasswordHintField;
import island.gotenyama.wand.web.ApplicationController;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * パスワードヒント画面を管理するアクションクラス
 * 
 * @author tk
 */
@Controller
public class PasswordHintAction extends ApplicationController {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 画面パス
	 */
	public static final String PAGE = "passwordHintIndex.vm";

	@Override
	protected String getDefaultPagePath() {
		return PAGE;
	}

	/**
	 * 初期表示する。
	 * 
	 * @return 遷移先ページ
	 */
	@RequestMapping(value = "/passwordHint/index.html", method = { RequestMethod.GET })
	public String index(ModelMap mm) {
		String sql = passwordHintQ.defSelectSQL + " order by code";
		List<DataRow> resultList = passwordHintQ.selectAll(sql, passwordHintQ.defMapper);
		mm.put("resultList", resultList);
		return PAGE;
	}

	/**
	 * 作成する。
	 * 
	 * @return 遷移先ページ
	 */
	@RequestMapping(value = "/passwordHint/create/result.xml", method = { RequestMethod.POST })
	public String index(@RequestParam("code") String code, @RequestParam("description") String description, ModelMap mm) {

		// NextPage
		setNextPage("passwordHintCreateResult.vm");

		// Validation
		val.evaluate(val.validate(PasswordHintField.CODE, code));

		// Insert
		QueryParams params = new QueryParams();
		params.add(PasswordHintField.CODE, code);
		params.add(PasswordHintField.DESCRIPTION, description);
		long id = passwordHintQ.insert(params);

		// SelectALL
		List<DataRow> resultList = passwordHintQ.selectAll(passwordHintQ.defSelectSQL, passwordHintQ.defMapper);
		mm.put("insertId", id);
		mm.put("resultList", resultList);
		return "passwordHintCreateResult.vm";
	}
}
